<template>
  <view
    :class="[
      styleType === 'text'
        ? 'segmented-control--text'
        : 'segmented-control--button',
    ]"
    :style="{ borderColor: styleType === 'text' ? '' : activeColor }"
    class="segmented-control"
  >
    <view
      v-for="(item, index) in values"
      :class="[
        styleType === 'text'
          ? 'segmented-control__item--text'
          : 'segmented-control__item--button',
        index === currentIndex && styleType === 'button'
          ? 'segmented-control__item--button--active'
          : '',
        index === 0 && styleType === 'button'
          ? 'segmented-control__item--button--first'
          : '',
        index === values.length - 1 && styleType === 'button'
          ? 'segmented-control__item--button--last'
          : '',
        index === currentIndex ? 'active-bottom' : '',
      ]"
      :key="index"
      class="segmented-control__item"
      @click="_onClick(index)"
    >
      <text
        :style="{
          color:
            index === currentIndex
              ? styleType === 'text'
                ? activeColor
                : '#fff'
              : styleType === 'text'
              ? '#A2A8A8'
              : activeColor,
        }"
        class="segmented-control__text"
        >{{ item }}</text
      >
    </view>
  </view>
</template>

<script>
export default {
  name: "UniSegmentedControl",
  props: {
    current: {
      type: Number,
      default: 0,
    },
    values: {
      type: Array,
      default() {
        return [];
      },
    },
    activeColor: {
      type: String,
      default: "#007aff",
    },
    styleType: {
      type: String,
      default: "button",
    },
  },
  data() {
    return {
      currentIndex: 0,
    };
  },
  watch: {
    current(val) {
      if (val !== this.currentIndex) {
        this.currentIndex = val;
      }
    },
  },
  created() {
    this.currentIndex = this.current;
  },
  methods: {
    _onClick(index) {
      if (this.currentIndex !== index) {
        this.currentIndex = index;
        this.$emit("clickItem", {
          currentIndex: index,
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/uni.scss";

.segmented-control {
  /* #ifndef APP-NVUE */
  display: flex;
  box-sizing: border-box;
  /* #endif */
  flex-direction: row;
  position: relative;
  &::after {
    width: 100%;
    content: " ";
    position: absolute;
    left: 0;
    bottom: 0;
    height: 1px;
    border-top: 1px solid #e6e6e6;
    color: #e6e6e6;
    -webkit-transform-origin: 0 0;
    transform-origin: 0 0;
    -webkit-transform: scaleY(0.5);
    transform: scaleY(0.5);
    z-index: 20;
  }
}

.segmented-control__item {
  /* #ifndef APP-NVUE */
  display: inline-flex;
  box-sizing: border-box;
  /* #endif */
  position: relative;
  flex: 1;
  justify-content: center;
  align-items: center;
}

.segmented-control__item--button {
  border-style: solid;
  border-top-width: 1px;
  border-bottom-width: 1px;
  border-right-width: 1px;
  border-left-width: 0;
}

.segmented-control__item--button--first {
  border-left-width: 1px;
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
}

.segmented-control__item--button--last {
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
}

.segmented-control__text {
  font-size: 28upx;
  font-family: PingFangSC-Semibold, PingFang SC;
  font-weight: 600;
  color: rgba(162, 168, 168, 1);
  text-align: center;
  padding-bottom: 30upx;
}

.active-bottom {
  position: relative;

  &::after {
    content: " ";
    width: 41upx;
    height: 6upx;
    background: #0075ff;
    border-radius: 2px;
    position: absolute;
    bottom: 5upx;
    left: 50%;
    -webkit-transform: translateX(-50%);
    transform: translateX(-50%);
    z-index: 30;
  }
}
</style>
